package com.huanghuang.shuadan.service.entity.base;

import com.huanghuang.shuadan.common.exception.ExceptionEnum;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;

/**
 * @Author suxiaoxiao
 * @Date 2019/7/13 21:51
 * @Version 1.0
 */
@Data
@ApiModel(description= "通用返回对象")
public class Result<T> implements Serializable {

    /**
     * 序列化 ID
     */
    private static final long serialVersionUID = -1324820919660967424L;

    /**
     * 接口是否调用成功
     */
    @ApiModelProperty(value = "接口是否调用成功")
    private Boolean success;

    /**
     * 错误码
     */
    @ApiModelProperty(value = "错误码")
    private String code;

    /**
     * 错误消息
     */
    @ApiModelProperty(value = "错误消息")
    private String message;

    /**
     * 返回值
     */
    @ApiModelProperty(value = "返回值")
    private T result;

    public Result(String code, String message) {
        success = false;
        this.code = code;
        this.message = message;
    }

    public Result(T result) {
        success = true;
        this.code = "200";
        this.message = "成功";
        this.result = result;
    }

    private Result(ExceptionEnum exceptionEnum) {
        success = false;
        this.code = exceptionEnum.getCode();
        this.message = exceptionEnum.getMessage();
    }

    public static <T> Result<T> error(String code, String message) {
        return new Result<>(code, message);
    }

    public static <T> Result<T> error(ExceptionEnum exceptionEnum) {
        return new Result(exceptionEnum);
    }

}
